package net.skystudio.es.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import net.skystudio.es.common.PageBean;
import net.skystudio.es.entity.ItemDesc;
import net.skystudio.es.mapper.ItemDescMapper;
import net.skystudio.es.service.ItemDescService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Order(2)
@Service(value = "itemDescForMysql")
public class ItemDescForMysqlServiceImpl implements ItemDescService {

    @Autowired
    private ItemDescMapper itemDescMapper;

    @Override
    public List<ItemDesc> findBySearch(String key) {
        QueryWrapper<ItemDesc> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", key).or().like("content", key);
        IPage<ItemDesc> page  = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(1, 10);
        IPage<ItemDesc> resultPage = itemDescMapper.selectPage(page, queryWrapper);
        return resultPage.getRecords();
    }

    @Override
    public PageBean<ItemDesc> pageQuery(Integer page, Integer size, String key) {
        QueryWrapper<ItemDesc> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", key).or().like("content", key);
        IPage<ItemDesc> p  = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page , size);
        IPage<ItemDesc> resultPage = itemDescMapper.selectPage(p, queryWrapper);
        PageBean pageBean = new PageBean();
        pageBean.setDatas(resultPage.getRecords());
        pageBean.setTotalRow(resultPage.getTotal());
        pageBean.setPageNum(resultPage.getCurrent());
        pageBean.setPageSize(resultPage.getSize());
        pageBean.setTotalPage(resultPage.getPages());
        return pageBean;
    }
}
